package com.ylb.rent.service.impl;

import java.beans.Transient;
import java.lang.invoke.WrongMethodTypeException;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sun.xml.internal.bind.v2.TODO;
import com.xiaoleilu.hutool.util.CollectionUtil;
import com.ylb.rent.dao.*;
import com.ylb.rent.entity.*;
import com.ylb.rent.entity.vo.RoomVo;
import com.ylb.rent.service.IRoomStuffService;
import com.ylb.rent.util.DateUtil;
import com.ylb.rent.util.R;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ylb.rent.service.IRoomService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 房间Service业务层处理
 * 
 * @author ylb
 * @date 2023-06-29
 */
@Service
@Slf4j
public class RoomServiceImpl implements IRoomService
{
    @Autowired
    private RoomMapper roomMapper;
    @Autowired
    private FileMapper fileMapper;
    @Autowired
    private RoomStuffMapper roomStuffMapper;

    @Autowired
    private RoomImageMapper roomImageMapper;


    @Autowired
    private BedMapper bedMapper;

    @Autowired
    private IRoomStuffService roomStuffService;


    @Override
    public RoomVo selectVoById(long id) {
        Room room = this.roomMapper.selectById(id);
        RoomVo roomVo = new RoomVo();
        roomVo.setRoom(room);
        Long roomId = room.getId();
//        TODO 房间的床
        Bed bed = bedMapper.selectById(room.getBedId());
        roomVo.setBed(bed);
//        TODO 房间设施
        List<Stuff> stuffs = roomStuffService.selectStuffListByRoomId(roomId);
        roomVo.setStuffList(stuffs);
//        TODO 房间图片
        List<String> imageList = roomImageMapper.selectImageListByRoomId(roomId);
        roomVo.setImageList(imageList);

//        TODO 开始/结束日期
        roomVo.setFromDate(new Date());
        Date plus = DateUtil.plus(LocalDateTime.now(), 1, ChronoUnit.MONTHS);
        roomVo.setToDate(plus);
        return roomVo;
    }

    @Override
    public List<RoomVo> selectVoListByApartmentId(long aId) {
        List<Room> rooms = this.selectListByApartmentId(aId);

        return rooms.stream().map(room -> {
            return this.selectVoById(room.getId());
        }).collect(Collectors.toList());

    }

    @Override
    public List<Room> selectListByApartmentId(Long aId) {
        Room build = Room.builder().apartmentId(aId).build();
        return roomMapper.selectRoomList(build);
    }

    /**
     * 查询房间
     * 
     * @param id 房间主键
     * @return 房间
     */
    @Override
    public RoomVo selectRoomById(Long id)
    {
        Room room=roomMapper.selectRoomById(id);
        RoomVo roomVo = new RoomVo();
        roomVo.setRoom(room);
        roomVo.setStuffList(roomStuffMapper.selectStuffIdByRoomId(room.getId()));
        RoomImage build = RoomImage.builder().roomId(id).build();
        if(CollectionUtil.isNotEmpty(roomImageMapper.selectRoomImageList(build))){
            roomVo.setImageList(roomImageMapper.selectImageListByRoomId(id));
        }
        return roomVo;
    }

    /**
     * 查询房间列表
     * 
     * @param room 房间
     * @return 房间
     */
    @Override
    public List<RoomVo> selectRoomList(Room room)
    {
        List<Room> list= roomMapper.selectRoomList(room);
        List<RoomVo> roomVos = new ArrayList<>();
        for(Room room1:list){
            RoomVo roomVo = new RoomVo();
            roomVo.setRoom(room1);
            Long id = room1.getId();
            roomVo.setStuffList(roomStuffMapper.selectStuffIdByRoomId(id));
            RoomImage build = RoomImage.builder().roomId(id).build();
            if(CollectionUtil.isNotEmpty(roomImageMapper.selectRoomImageList(build))){
                roomVo.setImageList(roomImageMapper.selectImageListByRoomId(id));
            }
            roomVos.add(roomVo);
        }
        return roomVos;
    }

    /**
     * 新增房间
     * 
     * @param roomVo 房间以及其关联的设施图片
     * @return 结果
     */
    @Transactional
    @Override
    public int insertRoom(RoomVo roomVo)
    {

        //添加房间
        System.out.println(roomVo.getImageList());
        Room room=roomVo.getRoom();
        int roomRes=roomMapper.insertRoom(room);
        //添加房间设施
        for(Stuff stuff:roomVo.getStuffList()){
            RoomStuff roomStuff = new RoomStuff();
            roomStuff.setRoomId(room.getId());
            roomStuff.setStuffId(stuff.getId());
            roomStuffMapper.insertRoomStuff(roomStuff);
        }
        //添加房间图片
        roomVo.getImageList().forEach(item->{
//            插入file
            File file = File.builder().filename(item).isDelete(0L).build();
            fileMapper.insertFile(file);
//            根据得到的file插入关联表
            RoomImage roomImage = RoomImage.builder().roomId(room.getId()).imageId(file.getId()).isDelete(0L).build();
            int result = roomImageMapper.insertRoomImage(roomImage);
            log.info("[insert]  aId:{} --- fileId: {}  [result]]={}",room.getId(),file.getId(),result);

        });
//        return roomMapper.insertRoom(room);
        return 1;
    }

    /**
     * 修改房间
     * 
     * @param roomVo 房间
     * @return 结果
     */
    @Transactional
    @Override
    public int updateRoom(RoomVo roomVo)
    {
        roomStuffMapper.deleteRoomStuffByRoomId(roomVo.getId());
        for(Stuff stuff:roomVo.getStuffList()){
            RoomStuff roomStuff = new RoomStuff();
            roomStuff.setRoomId(roomVo.getId());
            roomStuff.setStuffId(stuff.getId());
            roomStuffMapper.insertRoomStuff(roomStuff);
        }
        roomImageMapper.deleteRoomImageByRoomId(roomVo.getId());
        roomVo.getImageList().forEach(item->{
//            插入file
            File file = File.builder().filename(item).isDelete(0L).build();
            fileMapper.insertFile(file);
//            根据得到的file插入关联表
            RoomImage roomImage = RoomImage.builder().roomId(roomVo.getId()).imageId(file.getId()).isDelete(0L).build();
            int result = roomImageMapper.insertRoomImage(roomImage);
            log.info("[insert]  aId:{} --- fileId: {}  [result]]={}",roomVo.getId(),file.getId(),result);
        });
        //修改房间图片表

        return roomMapper.updateRoom(roomVo.getRoom());
    }

    /**
     * 批量删除房间
     * 
     * @param ids 需要删除的房间主键
     * @return 结果
     */
    @Override
    public int deleteRoomByIds(Long[] ids)
    {
        return roomMapper.deleteRoomByIds(ids);
    }

    /**
     * 删除房间信息
     * 
     * @param id 房间主键
     * @return 结果
     */
    @Override
    public int deleteRoomById(Long id)
    {
        return roomMapper.deleteRoomById(id);
    }
}
